﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate;
using NHibernate;
using NHibernate.Linq;
using System.Reflection;
using SHKurzy.Domain;
using System.Collections.ObjectModel;
using SHKurzy.Upsize;
using log4net;

namespace SHKurzy
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
private static readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
 


        protected SessionSource SessionSource { get; set; }
        protected ISession Session { get; private set; }

        public MainWindow()
        {
			log4net.Config.XmlConfigurator.Configure();

			log.Info("");
			log.Info("------------------------------------------------------------------------");
			log.Info("STARTUP");

            InitializeComponent();
        }












        //private static ISessionFactory CreateSessionFactory()
        //{
        //    return Fluently.Configure()
        //      .Database(
        //        SQLiteConfiguration.Standard
        //          .UsingFile("firstProject.db")
        //      )
        //      .Mappings(m =>
        //        m.FluentMappings.AddFromAssemblyOf<MainWindow>())
        //      .ExposeConfiguration(BuildSchema)
        //      .BuildSessionFactory();
        //}


        private static ISessionFactory CreateSessionFactory(string SqLiteDatabaseFile)
        {
            return Fluently.Configure()
                .Database(SQLiteConfiguration.Standard.UsingFile(SqLiteDatabaseFile))
                .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
                .BuildSessionFactory();
        }

        //private static void BuildSchema(Configuration config)
        //{
        //    string DbFile = "firstProject.db";
        //    // delete the existing db on each run
        //    if (File.Exists(DbFile))
        //        File.Delete(DbFile);

        //    // this NHibernate tool takes a configuration (with mapping info in)
        //    // and exports a database schema from it
        //    new SchemaExport(config)
        //      .Create(false, true);
        //}








        private void button1_Click(object sender, RoutedEventArgs e)
        {

                          //     addMappingsFromAssembly(typeof(CustomerMapping).Assembly);



            //        var cfg = Fluently.Configure()
            //.Database(SQLiteConfiguration.Standard.InMemory);
            //        SessionSource = new SessionSource((cfg.BuildConfiguration().Properties);
            //        Session = SessionSource.CreateSession();
            //        SessionSource.BuildSchema(Session);


            var sessionFactory = CreateSessionFactory(@"C:\src\svh\Svh\test_database\test.db3");

            using (var session = sessionFactory.OpenSession())
            {

                

                using (var transaction = session.BeginTransaction())
                {
                    var a = session.CreateCriteria(typeof(Kurz)).List<Kurz>();

                    //foreach (Kurz k in a)
                    //{
                    //    Title = k.Nazev;
                    //}



                    var b = from m in session.QueryOver<Kurz>() where m.Cena > 100 select m;

                    //var c = b.to

                    var c = from x in session.Query<Kurz>() where x.Cena > 99 select x;
                    var d = c.ToList();


                    var f = from x in session.Query<Kurz>() where x.Nazev.Contains("kurz") select x;


                    var dc = from x in session.Query<Kurz>() select x;

                    var obs = new ObservableCollection<Kurz>(dc.ToList())                        ;
                    

                    dataGrid1.AutoGenerateColumns = true;
                    dataGrid1.ItemsSource = obs;



                    transaction.Commit();
                }
            }
        }

		private void button2_Click(object sender, RoutedEventArgs e)
		{
			
			
			// D:\tmp\svh\svh_pokus.mdb 

			var upsizeWin = new UpsizeWin { Owner = this };
			upsizeWin.ShowDialog();
		}

	


    }
}